package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.result.Result;
import com.system.insurance.entity.AppletUser;
import com.system.insurance.entity.Frequency;
import com.system.insurance.service.AppletUserService;
import com.system.insurance.service.FrequencyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 次数 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-09
 */
@Api(tags = "次数")
@RestController
@RequestMapping("/insurance/frequency")
public class FrequencyController {


    @Autowired
    private FrequencyService frequencyService;

    @Autowired
    private AppletUserService appletUserService;

    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "0 */1 * * * ?")
    @ApiOperation(value = "每天定时设置新的次数")
    @PostMapping("/updateNmu")
    public Result updateNum(){
        List<Frequency> list = frequencyService.list();

        for (int i = 0; i < list.size(); i++) {

            AppletUser byId = appletUserService.getById(list.get(i).getAppletuserid());
            list.get(i).setEnquiry(byId.getNum());
            list.get(i).setOffer(byId.getBjnum());

            list.get(i).setUpdateTime(LocalDateTime.now());
            boolean byId1 = frequencyService.updateById(list.get(i));

            if (byId1){
                System.out.println("=================修改成功==================");
                return Result.ok().message("设置成功");
            }
            return Result.fail().message("修改失败");
        }
        return Result.fail().message("暂无数据!");
    }

}

